﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace YG.CommonFuncs
{
    /// <summary>
    /// json转换成dataset
    /// </summary>
    public static class Data2Json
    {
        public static DataSet ToDataSet(object[] objArr)
        {
            if (objArr.Length == 0)
                return null;

            Type className = objArr[0].GetType();
            //Fill(ds, className, source);

            DataSet ds = CreateDataSet(objArr[0].GetType());
            ds = FillDataSet(ds, objArr);
            return ds;
        }

        public static DataSet CreateDataSet(Type t)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);

            System.Reflection.PropertyInfo[] pis = t.GetProperties();
            foreach (System.Reflection.PropertyInfo pi in pis)
            {
                DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
                dt.Columns.Add(dc);
            }
            return ds;
        }

        public static DataSet FillDataSet(DataSet ds, object[] objArr)
        {
            DataColumnCollection dcs = ds.Tables[0].Columns;
            Type t = objArr[0].GetType();
            foreach (object obj in objArr)
            {
                DataRow dr = ds.Tables[0].NewRow();
                for (int i = 0; i < dcs.Count; i++)
                {
                    dr[i] = t.InvokeMember(dcs[i].ColumnName, System.Reflection.BindingFlags.GetProperty, null, obj, null);
                }
                ds.Tables[0].Rows.Add(dr);
            }
            return ds;
        }

        public static List<T> PutAllVal<T>(T entity, DataSet ds) where T : new()
        {
            List<T> lists = new List<T>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    lists.Add(PutVal(new T(), row));
                }
            }
            return lists;
        }

        public static T PutVal<T>(T entity, DataRow row) where T : new()
        {
            //初始化 如果为null
            if (entity == null)
            {
                entity = new T();
            }
            //得到类型
            Type type = typeof(T);
            //取得属性集合
            PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo item in pi)
            {
                //给属性赋值
                if (row[item.Name] != null && row[item.Name] != DBNull.Value)
                {
                    if (item.PropertyType == typeof(System.Nullable<System.DateTime>))
                    {
                        item.SetValue(entity, Convert.ToDateTime(row[item.Name].ToString()), null);
                    }
                    else
                    {
                        item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                    }
                }
            }
            return entity;
        }
    }
}